package com.mudalov.rec.dao.hibernate.impl;

import org.hibernate.SessionFactory;
import org.hibernate.Session;

import com.mudalov.rec.dao.interfaces.HbnGenericDAO;
import com.mudalov.rec.dao.interfaces.IMaterialDAO;
import com.mudalov.rec.dao.model.study.Material;
import java.util.ArrayList;
import java.util.List;

public class HbnMaterialDAO extends HbnGenericDAO<Material, Long> implements
		IMaterialDAO {

	public HbnMaterialDAO(SessionFactory sesssionFactory) {
		this.sessionFactory = sesssionFactory;
	}

	@Override
	protected Class<Material> getEntityClass() {
		return Material.class;
	}
    
    public List<Material> getMaterials(String name, String owner, String subject) {
           String query = "from Material m where m.owner.fullName like :ownerName " +
            "and m.name like :name " +
            "and m.subject.text like :subjectText";
        Session session = sessionFactory.openSession();
        List<Material> result = session.createQuery(query)
                .setString("name", "%" + name + "%")
                .setString("ownerName", "%" + owner + "%")
                .setString("subjectText", "%" + subject + "%").list();
        List<Material> detachedResult = new ArrayList<Material>();
        for (Material test : result) {
            detachedResult.add(test.getClone());
        }
        session.close();
        return detachedResult; 
    }

}
